package tdh.dao.Impl;

import org.hibernate.*;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import tdh.dao.UserDao;
import tdh.entity.po.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用户类数据库操作接口实现类
 * @author 曹昊
 * @date 2021/7/12
 */
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Autowired
    SessionFactory sessionFactory;


    /*
     * @Author caohao
     * @Description 查询方法
     * @Date 11:43 2021/7/13
     * @Param [user]
     * @return java.util.List<tdh.entity.po.User>
     **/
    @Override
    public List<User> select(User user) {
        ArrayList<User> users = new ArrayList<User>();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(User.class);
        if (user.getYhid()!=null&&!user.getYhid().equals("")){
            criteria.add(Restrictions.eq("yhid",user.getYhid()));
        }
        if (user.getYhdm()!=null&&!user.getYhdm().equals("")){
            criteria.add(Restrictions.eq("yhdm",user.getYhdm()));
        }
        if (user.getYhxm()!=null&&!user.getYhxm().equals("")){
            criteria.add(Restrictions.eq("yhxm",user.getYhxm()));
        }
        if (user.getDwdm()!=null&&!user.getDwdm().equals("")){
            criteria.add(Restrictions.eq("dwdm",user.getDwdm()));
        }
        if (user.getYhkl()!=null&&!user.getYhkl().equals("")){
            criteria.add(Restrictions.eq("yhkl",user.getYhkl()));
        }
        if (user.getYhxb()!=null&&!user.getYhxb().equals("")){
            criteria.add(Restrictions.eq("yhxb",user.getYhxb()));
        }
        if (user.getYhbm()!=null&&!user.getYhbm().equals("")){
            criteria.add(Restrictions.eq("yhbm",user.getYhbm()));
        }
        List list =criteria.list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();){
            User tmp = (User) iterator.next();
            users.add(tmp);
        }
        session.close();
        return users;
    }


    /**
     * @Author caohao
     * @Description 根据用户姓名查询全部的用户列表
     * @Date 10:02 2021/7/12
     * @Param []
     * @return java.util.List<tdh.entity.po.User>
     **/
    @Override
    public List<User> yhxmUsers(String yhxm) {
        String hql = "from  tdh.entity.po.User u where u.yhxm like '"+yhxm+"%'";
        ArrayList<User> users = new ArrayList<User>();
        Session session = sessionFactory.openSession();
        List list = session.createQuery(hql).list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();){
            User user = (User) iterator.next();
            users.add(user);
        }
        session.close();
        return users;
    }

    @Override
    public List<User> yhidUsers(String yhid) {
        String hql = "from  tdh.entity.po.User u where u.yhid like '"+yhid+"%'";
        ArrayList<User> users = new ArrayList<User>();
        Session session = sessionFactory.openSession();
        List list = session.createQuery(hql).list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();){
            User user = (User) iterator.next();
            users.add(user);
        }
        session.close();
        return users;
    }


    /**
     * @Author caohao
     * @Description 插入新的用户行
     * @Date 10:03 2021/7/12
     * @Param [user]
     * @return void
     **/
    @Override
    public void insertOne(User user) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.save(user);
        transaction.commit();
        session.close();
    }

    /**
     * @Author caohao
     * @Description 根据主键删除指定用户信息
     * @Date 10:03 2021/7/12
     * @Param [yhdm]
     * @return void
     **/
    @Override
    public void deleteOne(String yhdm) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        User user = new User();
        user.setYhdm(yhdm);
        session.delete(user);
        transaction.commit();
        session.close();
    }

    /**
     * @Author caohao
     * @Description 更新指定用户的某些列信息
     * @Date 10:03 2021/7/12
     * @Param [user]
     * @return void
     **/
    @Override
    public void updateUser(User user) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(user);
        transaction.commit();
        session.close();
    }
}
